<%--
  Created by IntelliJ IDEA.
  User: Infor
  Date: 2017/07/03
  Time: 19:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<html>
<head>
    <title>Student List</title>
</head>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<body>
<div>
    <a href="<c:url value="/student/add"/>">新增</a>
</div>

<div>
    <c:url var="studentlist" value="/student/list"/>
    <f:form action="${studentlist}" modelAttribute="filter" method="get" acceptCharset="utf-8">
        姓名: <input name="name" value="${filter.name}"><br/>
        电话: <input name="phone" value="${filter.phone}"><br/>
        Email: <input name="email" value="${filter.email}"><br/>

        年龄: <input name="minAge" value="${filter.minAge}"> - <input name="maxAge" value="${filter.maxAge}"> <br/>
        <%-- 创建日期: <input type="date" name="minDate" value="${filter.minDate}"> - <input type="date" name="maxDate"
                                                                                     value="${filter.maxDate}"> <br/>--%>
        页码: <input name="pageIndex" value="${filter.pageIndex}"> 每页: <input name="pageSize"
                                                                            value="${filter.pageSize}"> 条<br/>
        <input type="submit" value="提交"/>
    </f:form>
</div>

<c:choose>
<c:when test="${students!=null && students.size()==0}">
    <p>没有数据啦</p>
</c:when>
<c:otherwise>
<table border="1">
    <tr>
        <th>id</th>
        <th>name</th>
        <th>age</th>
        <th>phone</th>
        <th>email</th>
        <th>createDate</th>
        <th>操作</th>
    </tr>
    <c:forEach items="${students}" var="item">
        <tr>
            <td>${item.id}</td>
            <td>${item.name}</td>
            <td>${item.age}</td>
            <td>${item.phone}</td>
            <td>${item.email}</td>
            <td><fmt:formatDate value="${item.createDate}" pattern="YYYY-MM-dd hh:mm:ss"/></td>
            <td>
                <div>
                    <a href="<c:url value="/student/edit/${item.id}"/>">编辑</a>
                    <a id="delete" data-id="${item.id}" href="#">删除</a>
                </div>
            </td>
        </tr>
    </c:forEach>
    </c:otherwise>
    </c:choose>
</table>
</body>
</html>

<script>
    $(function () {
        $("#delete").click(function () {
            var id = $(this).data("id");
            var url = '<c:url value="/student/api/delete/"/>' + id;
            $.ajax({
                url: url,
                type: 'post',
                success: function (data) {
                    if (data.success) {
                        alert("delete " + id + " success");
                        window.location.reload();
                    }
                    else
                        alert("delete " + id + " fail");

                },
                error: function () {
                    alert("delete " + id + " fail");
                }
            })
        })
    })
</script>